﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Snowdreamist.Diagnostics;
using Snowdreamist.Runtime;
using Snowdreamist.Runtime.Services;

namespace Snowdreamist.Windows.Controls
{
    public class ComposeDataTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item == null)
                return null;

            IDataTemplateManager manager = null;

            if (this.DataTemplateManager != null)
            {
                return this.DataTemplateManager.GetTemplate(item.GetType(), this.DataTemplateScope);
            }
            else if (SystemRuntime.Current.TryGetService<IDataTemplateManager>(out manager))
            {
                return manager.GetTemplate(item.GetType(), this.DataTemplateScope);
            }
            else
            {
                // log
                SystemRuntime.Current.Log.Write(TraceEventType.Warning, (int)EventID.LoadDataTemplateFailed, "IDataTemplateManager runtime service not found");
                return null;
            }
        }

        /// <summary>
        /// Scope
        /// </summary>
        public string DataTemplateScope { get; set; }

        /// <summary>
        /// DataTemplateManager
        /// </summary>
        public IDataTemplateManager DataTemplateManager { get; set; }

        /// <summary>
        /// Select a template
        /// </summary>
        /// <param name="item"></param>
        /// <param name="container"></param>
        /// <param name="viewState"></param>
        public virtual DataTemplate SelectTemplate(object item, DependencyObject container, string viewState)
        {
            throw new NotImplementedException();
        }
    }
}